﻿using System;
using System.ServiceModel.Channels;

namespace Microsoft.Biztalk.Adapters.Wcf.NullBinding
{
    class NullBindingTransportBindingElement : TransportBindingElement, ITransactedBindingElement
    {
        NullBindingProperties properties;
        public NullBindingTransportBindingElement(NullBindingProperties properties)
        {
            this.properties = properties.Clone();
        }

        public override string Scheme
        {
            get { return Helpers.BindingScheme; }
        }

        public override BindingElement Clone()
        {
            return new NullBindingTransportBindingElement(this.properties);
        }

        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
        {
            if (!CanBuildChannelFactory<TChannel>(context))
                throw new NotSupportedException();

            if (typeof(TChannel) == typeof(IOutputChannel))
                return (IChannelFactory<TChannel>)(new NullChannelFactory<IOutputChannel>(this.properties));
            else
                return (IChannelFactory<TChannel>)(new NullChannelFactory<IRequestChannel>(this.properties));
        }

        public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
        {
            if (!CanBuildChannelListener<TChannel>(context))
                throw new NotSupportedException();
             
            return (IChannelListener<TChannel>)(new NullChannelListener<TChannel>(this.properties));
        }

        public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
        {
            if (typeof(TChannel) == typeof(IOutputChannel))
            {
                return true;
            }
            else if ((typeof(TChannel) == typeof(IRequestChannel)) && !properties.IsQueued)
            {
                return true;
            }
            else return false;
        }

        public override bool CanBuildChannelListener<TChannel>(BindingContext context)
        {
            if (typeof(TChannel) == typeof(IInputChannel))
            {
                return true;
            }
            else if ((typeof(TChannel) == typeof(IReplyChannel)) && !properties.IsQueued)
            {
                return true;
            }
            else return false;
        }

        public override long MaxBufferPoolSize
        {
            get
            {
                throw new NotSupportedException("Max Buffer Pool Size is unsupported");
            }
            set
            {
                throw new NotSupportedException("Max Buffer Pool Size is unsupported");
            }
        }

        public override long MaxReceivedMessageSize
        {
            get
            {
                return base.MaxReceivedMessageSize;
            }
            set
            {
                base.MaxReceivedMessageSize = value;
            }
        }

        public bool TransactedReceiveEnabled
        {
            get 
            {
                if (this.properties.IsTransacted && this.properties.IsQueued)
                {
                    return true;
                }
                return false;
            }
        }
    }
}
